package com.lh.api.controller.order;

import com.lh.api.annotation.Login;
import com.lh.api.annotation.OrderAnnotation;
import com.lh.api.controller.AbstractController;
import com.lh.api.service.PayService;
import com.lh.api.vo.PayConfigVo;
import com.lh.api.vo.VerifyVo;
import com.lh.common.utils.HttpUtil;
import com.lh.common.utils.Result;
import com.paypal.base.rest.PayPalRESTException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Gze
 * @ClassName: PayAction
 * @Description:
 * @date: 2020-05-25 15:21
 */

@Api(value="PayAction",tags={"支付相关接口"})
@RestController
@RequestMapping("/api/pay")
public class PayAction extends AbstractController {

    @Autowired
    private PayService payService;

    @ApiOperation(value = "获取支付配置")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "orderItem", value = "订单号", required = true,dataTypeClass= String.class)
    })
    @GetMapping("/getPayConfig")
    @ResponseBody
    @Login
    public Result<PayConfigVo> getPayConfig(HttpServletRequest request, String orderItem){
        return Result.ok(payService.getPayConfig(orderItem, getUserId(), request));
    }

    @ApiOperation(value = "支付回调接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "paymentId", value = "PayPal付款订单id，形如：PAY-0US81985GW1191216KOY7OXA", required = true,dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "orderItem", value = "订单号", required = true,dataTypeClass= String.class)
    })
    @GetMapping("/success")
    @ResponseBody
    @Login
    public Result<VerifyVo> success(String paymentId, String orderItem) throws PayPalRESTException {
        return  Result.ok(payService.success(paymentId, orderItem, getUserId()));

    }

    @ApiOperation(value = "Webhook支付回调")
    @RequestMapping("/orderPayCallback")
    public Result<String> orderPayCallback(HttpServletRequest request ) {
        payService.orderPayCallback(HttpUtil.getRequestString(request));
        return Result.ok();
    }

}
